//
//  RelationViewModel.m
//  Yihubang
//
//  Created by xmy on 16/8/30.
//  Copyright © 2016年 jinghao. All rights reserved.
//

#import "RelationViewModel.h"
#import "relationCustomCell.h"
@interface RelationViewModel()
@property (nonatomic, strong) NSIndexPath * selectHidden;
@property (nonatomic,strong,readwrite)RACCommand *saveInfoCommend;

@end
@implementation RelationViewModel
- (void)initialize
{
    [super initialize];
    @weakify(self)
    [[[RACObserve(self, dataSource)
       filter:^(NSArray *events) {
           return @(events.count > 0).boolValue;
       }]
      deliverOnMainThread]
     subscribeNext:^(NSArray *events) {
         @strongify(self)
         //         [self.mTableView beginUpdates];
         //         self.imgArr =self.dataSource[0][0];
         [self.mTableView reloadData];
         //         [self.mTableView endUpdates];
     }];
    self.saveInfoCommend = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSArray *data) {
        @strongify(self)
        if (self.viewController.callbackBlock) {
            self.viewController.callbackBlock(self.relation);
            [self.viewController gotoBack];
        }
        return [RACSignal empty];
    }];

}
-(NSString *)relation{
    if(!_relation){
        _relation =[NSString string];
    }
    return _relation;
}
- (void)bindTableView:(UITableView *)tableView
{
    [super bindTableView:tableView];
    self.cellReuseIdentifier = @"relationCustomCell";
    [self.mTableView registerCellWithReuseIdentifier:@"relationCustomCell"];
}
- (void)configureCell:(relationCustomCell *)cell atIndexPath:(NSIndexPath *)indexPath withObject:(id)object
{
     NSArray * arr =self.dataSource[0];
    if(indexPath.row < arr.count-1){
        cell.select_img.hidden=YES;
        cell.customRelation.hidden=YES;
        cell.relation_label.hidden=NO;
        cell.relation_label.text =arr[indexPath.row];
    }else{
        cell.select_img.hidden=YES;
        cell.customRelation.hidden=NO;
        cell.relation_label.hidden=YES;
        @weakify(self);
        [[cell.customRelation.rac_textSignal filter:^BOOL(NSString * value){
            return value.length > 0;
        }]subscribeNext:^(NSString*name) {
            @strongify(self);
            self.relation =name;
        }];
    }
    
    cell.customRelation.userInteractionEnabled=NO;
    if(self.selectHidden ==nil){
        if(self.relation.length >0){
            if([arr containsObject:self.relation]){
                if([object isEqual:self.relation]){
                    cell.select_img.hidden=NO;
                }else{
                    cell.select_img.hidden=YES;
                }
            }else{
                if(indexPath.row ==arr.count-1){
                    cell.select_img.hidden=NO;
                    cell.customRelation.text=self.relation;
                    cell.customRelation.userInteractionEnabled=YES;
                    [cell.customRelation becomeFirstResponder];
                }else{
                    cell.select_img.hidden=YES;
                }
            }
        }else{
            if(indexPath.row ==arr.count-1){
                cell.select_img.hidden =NO;
                cell.customRelation.userInteractionEnabled=YES;
//                [cell.customRelation becomeFirstResponder];
            }else{
                cell.select_img.hidden =YES;
            }
        }
    }else{
        if ([indexPath isEqual:self.selectHidden]){
            cell.relation_label.textColor = [UIColor colorWithHex:0x21C7B6];
            cell.select_img.hidden =NO;
            if(indexPath.row == arr.count-1){
                cell.customRelation.userInteractionEnabled=YES;
                [cell.customRelation becomeFirstResponder];
                cell.customRelation.textColor = [UIColor colorWithHex:0x21C7B6];
            }
        }else{
            cell.select_img.hidden=YES;
            cell.relation_label.textColor = [UIColor blackColor];
            cell.customRelation.textColor = [UIColor blackColor];
        }
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    self.selectHidden = indexPath;
    NSArray * arr =self.dataSource[0];
    if(indexPath.row < arr.count -1){
        self.relation = self.dataSource[0][indexPath.row];
    }
    [self.mTableView reloadData];
}

@end
